package example2;

import org.junit.Test;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 处理字符的节点流
 */
public class FileReaderWriterTest {
    @Test
    public void read() {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("1.txt");
            char[] chars = new char[10];
            
            int num = fileReader.read(chars);
            while (num != -1) {
                String str = new String(chars, 0, num);
                System.out.print(str);
                num = fileReader.read(chars);
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        } 
        finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            fileReader = new FileReader("1.txt");
            fileWriter = new FileWriter("2.txt");
            char[] chars = new char[10];
            int num = fileReader.read(chars);
            while (num != -1) {
                fileWriter.write(chars, 0, num);
                num = fileReader.read(chars);
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        } 
        finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}



